<template>
  <div>
    <el-dialog title="分配角色" :visible="isRole" @close="closeFn">
      <!-- 内容 -->
      <el-checkbox-group v-model="roleList">
        <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">
          {{ item.title }}
        </el-checkbox>
      </el-checkbox-group>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button size="samll" type="primary" @click="confirm">确 定</el-button>
        <el-button size="samll" @click="closeFn">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRolesList } from '@/api/employees'
import { getOneRoles, editRolesApi } from '@/api/employees'
export default {
  props: {
    isRole: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      checkList: [], // 角色列表
      roleList: [], // 该员工所分配的角色
      roleId: 0
    }
  },
  async created() {
    // 调用接口获取角色列表
    const res = await getRolesList()
    this.checkList = res.data
  },
  methods: {
    // 点击确认新增
    async confirm() {
      // 调用接口实现角色分配
      await editRolesApi(
        {
          id: this.roleId,
          roles: this.roleList
        }
      )
      this.closeFn()
    },
    // 员工的角色
    async  getRoles(id) {
      //   console.log('1')
    //   该用户的id
      this.roleId = id
      //   调用接口，获取该用户的数据
      const res = await getOneRoles(id)
      const result = res.data.roles
      const roles = []
      //   把每个权限id放到数组里
      for (let index = 0; index < result.length; index++) {
        roles.push(result[index].id)
      }
      // 回显复选框
      this.roleList = roles
    },
    // 关闭模态框
    closeFn() {
      // console.log(1)
      this.$emit('update:isRole', false)
    }
  }
}
</script>

<style>

</style>
